﻿using Unity.Burst;
using Unity.Mathematics;

namespace Game.FlowField.Burst
{
    [BurstCompile]
    public struct Unit
    {
        public int GridIndexFrom;
        public int GridIndexTo;
        /// <summary>
        /// 从<see cref="GridIndexFrom"/>移动到<see cref="GridIndexTo"/>的进度。
        /// </summary>
        public float MovingProgress;
        /// <summary>
        /// 上下左右移动为1，左上、右上、左下、右下移动为1/sqrt(2)
        /// </summary>
        public float MovingProgressFactor;
        /// <summary>
        /// 从<see cref="GridIndexFrom"/>的坐标。
        /// </summary>
        public float2 PositionFrom;
        /// <summary>
        /// 从<see cref="GridIndexFrom"/>移动到<see cref="GridIndexTo"/>的向量。
        /// </summary>
        public float2 Direction;
        /// <summary>
        /// Unit运动状态
        /// </summary>
        public UnitMotion Motion;
        /// <summary>
        /// 是否存活着
        /// </summary>
        public bool IsAlive;

        public float2 Position;

        [BurstCompile]
        public bool CanResetAsNewOne() => !this.IsAlive;

        [BurstCompile]
        public void ResetAsNewOne()
        {
            this.Motion = UnitMotion.Stay;
            this.IsAlive = true;
        }

        [BurstCompile]
        public void ResetAsDeadOne()
        {
            this.IsAlive = false;
        }

        [BurstCompile]
        public void Setup(int gridIndex, float2 position)
        {
            this.GridIndexFrom = gridIndex;
            this.GridIndexTo = gridIndex;
            this.PositionFrom = position;
            this.Position = position;
            this.Direction = new float2(0f, 0f);
        }
    }
}